package cn.dzhy.myzf.qq.email;

import cn.dzhy.myzf.qq.email.api.GroupApi;
import cn.dzhy.myzf.qq.email.api.PartyApi;
import cn.dzhy.myzf.qq.email.api.UserApi;
import cn.dzhy.myzf.qq.email.model.Group;
import cn.dzhy.myzf.qq.email.model.Party;
import cn.dzhy.myzf.qq.email.model.User;
import cn.dzhy.myzf.qq.email.model.UserStatus;
import cn.dzhy.myzf.qq.email.token.EmailTokenCreator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.cache.CacheStorager;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
import java.math.BigInteger;
import java.util.List;


/**
 * 腾讯企业邮 接口实现代理类
 * @className EmailProxy
 * @author myzf(yangzhuangfei_min@odao.com)
 * @date 2018/3/15 下午10:53
 * @since JDK 1.8
 * @see cn.dzhy.myzf.qq.email
 * @return
 * @throws WeixinException
 */

public class EmailProxy {

    /**
     * 成员API
     */
    private final UserApi userApi;
    /**
     * 部门API
     */
    private final PartyApi partyApi;
    /**
     * 邮件群组API
     */
    private final GroupApi groupApi;
    /**
     * token管理
     */
    private final TokenManager tokenManager;
    /**
     * 账号信息
     */
    private final WeixinAccount weixinAccount;
    /**
     * token存储
     */
    private final CacheStorager<Token> cacheStorager;
    /**
     * 微信接口实现(使用weixin4j.properties配置的account账号信息,
     * 使用FileCacheStorager文件方式缓存TOKEN)
     */
    public EmailProxy() {
        this(new FileCacheStorager<Token>());
    }

    /**
     * 微信接口实现(使用weixin4j.properties配置的account账号信息)
     *
     * @param cacheStorager
     *            token管理
     */
    public EmailProxy(CacheStorager<Token> cacheStorager) {
        this(Weixin4jConfigUtil.getWeixinAccount(), cacheStorager);
    }

    /**
     * 腾讯企业邮 接口实现
     *
     * @param weixinAccount
     *            账号信息
     * @param cacheStorager
     *            token管理
     */
    public EmailProxy(WeixinAccount weixinAccount,
                       CacheStorager<Token> cacheStorager) {
        this(weixinAccount, new EmailTokenCreator(weixinAccount.getId(),
                weixinAccount.getSecret()), cacheStorager);
    }

    private EmailProxy(WeixinAccount weixinAccount, TokenCreator tokenCreator,
                        CacheStorager<Token> cacheStorager) {
        if (weixinAccount == null) {
            throw new IllegalArgumentException(
                    "weixinAccount must not be empty");
        }
        if (tokenCreator == null) {
            throw new IllegalArgumentException("tokenCreator must not be empty");
        }
        if (cacheStorager == null) {
            throw new IllegalArgumentException(
                    "cacheStorager must not be empty");
        }
        this.tokenManager = new TokenManager(tokenCreator, cacheStorager);
        this.weixinAccount = weixinAccount;
        this.cacheStorager = cacheStorager;
        this.userApi = new UserApi(tokenManager);
        this.partyApi = new PartyApi(tokenManager);
        this.groupApi = new GroupApi(tokenManager);
    }
    /**
     * token获取
     *
     * @return
     */
    public TokenManager getTokenManager() {
        return this.tokenManager;
    }
    /**
     * 获取企业邮箱账号信息
     *
     * @return
     */
    public WeixinAccount getWeixinAccount() {
        return weixinAccount;
    }

    /**
     * 创建成员
     */
    public ApiResult createUser(User user) throws WeixinException {
        return userApi.createUser(user);
    }

    /**
     * 更新用户(如果非必须的字段未指定 则不更新该字段之前的设置值)
     */
    public ApiResult updateUser(User user) throws WeixinException {
        return userApi.updateUser(user);
    }


    /**
     * 获取成员
     */
    public User getUser(String userid) throws WeixinException {
        return userApi.getUser(userid);
    }

    /**
     *获取部门成员,获取部门成员详情
     * */
    public List<User> listUser(Long partyId, boolean fetch_child, boolean findDetail) throws WeixinException {
        return userApi.listUser(partyId,fetch_child,findDetail);
    }

    /**
     * 删除成员
     */
    public ApiResult deleteUser(String userid) throws WeixinException {
        return userApi.deleteUser(userid);
    }


    /**
     * 批量检查帐号
     */
    public List<UserStatus> batchcheckUser(List<String> userlist) throws WeixinException {
        return userApi.batchcheckUser(userlist);
    }


    /**
     * 创建部门(根部门的parentid为1)
     */
    public int createParty(Party party) throws WeixinException {
        return partyApi.createParty(party);
    }

    /**
     * 更新部门(如果非必须的字段未指定 则不更新该字段之前的设置值)
     */
    public ApiResult updateParty(Party party) throws WeixinException {
        return partyApi.updateParty(party);
    }

    /**
     * 删除部门(不能删除根部门；不能删除含有子部门、成员的部门)
     */
    public ApiResult deleteParty(BigInteger partId) throws WeixinException {
        return partyApi.deleteParty(partId);
    }

    /**
     * 查询部门列表(以部门的order字段从小到大排列)
     */
    public List<Party> listParty(BigInteger partId) throws WeixinException {
       return partyApi.listParty(partId);
    }

    /**
     *查找部门
     * */
    public List<Party> searchParty(Party party) throws WeixinException {
     return partyApi.Partysearch(party);
    }

    /**
     * 创建邮件群组
     */
    public ApiResult createGroup(Group group) throws WeixinException {
        return groupApi.createGroup(group);
    }

    /**
     * 更新邮件群组
     */
    public ApiResult updateGroup(Group group) throws WeixinException {
        return groupApi.updateGroup(group);
    }

    /**
     * 删除邮件群组
     */
    public ApiResult deleteGroup(String groupId) throws WeixinException {

        return groupApi.deleteGroup(groupId);
    }



    /**
     * 获取邮件群组信息
     */
    public Group getGroup(String groupid) throws WeixinException {
        return groupApi.getGroup(groupid);
    }





}
